<template>
  <view class="container">
    <view class="title">
      <view class="title1"> {{ title1 }}</view>
      <view class="sub_title">
        最长可以{{ subTitle }}
        <text style="color: #4095e5; margin-left: 5px">示例文件</text>
      </view>
    </view>
    <view class="choose_box">
      <view class="item1" @tap="handleType(1)" v-if="chooseType == '1'">
        <view class="right">
          <view class="type_title">交易类型</view>
          <view v-if="typeName" class="typeName">{{ typeName }}</view>
          <uni-icons class="toright" type="forward" size="16"></uni-icons>
        </view>
      </view>
      <view class="item1">
        <view class="right">
          <view class="type_title">账单时间</view>
        </view>
        <view class="tag_box">
          <uni-tag
            class="tag_item1"
            :type="type1"
            text="近一周"
            :inverted="true"
            @click="setType1('week')"
          />
          <uni-tag
            class="tag_item1"
            :type="type2"
            text="近一月"
            :inverted="true"
            @click="setType2('mouth')"
          />
          <uni-tag
            class="tag_item1"
            :type="type3"
            text="自定义时间"
            :inverted="true"
            @click="setType3('anytime')"
          />
        </view>
      </view>
      <view v-if="type1 == 'success' || type2 == 'success'" class="time">{{
        weektime
      }}</view>
      <!-- 自定义时间 -->
      <view class="item1" @tap="selectDate1" v-if="type3 == 'success'">
        <view class="right">
          <view class="type_title">起始时间</view>
          <view class="sdate">{{ startDate }}</view>
          <uni-icons class="toright" type="forward" size="16"></uni-icons>
        </view>
      </view>
      <view
        class="item1"
        style="border: none"
        @tap="selectDate2"
        v-if="type3 == 'success'"
      >
        <view class="right">
          <view class="type_title">结束时间</view>
          <view class="sdate">{{ endDate }}</view>
          <uni-icons class="toright" type="forward" size="16"></uni-icons>
        </view>
      </view>
    </view>

    <view
      class="btn3"
      :disabled="type1 == 'success' || type2 == 'success' || type3 == 'success'"
      @tap="toEmail"
      >下一步</view
    >
    <!-- 筛选弹窗 -->
    <uni-popup
      ref="popup1"
      class="popup1"
      type="bottom"
      background-color="#fff"
    >
      <view
        class="popup-content"
        :class="{ 'popup-height': type === 'left' || type === 'right' }"
      >
        <view class="pop_title">选择交易类型</view>
        <view class="tag_box1">
          <uni-tag
            v-for="item in tagList"
            :key="item.index"
            class="tag_item"
            :class="!item.isAble && item.type == 'success' ? 'active' : ''"
            :type="item.type"
            :text="item.name"
            :inverted="true"
            :disabled="item.isAble"
            @click="setTagListType(item.name)"
          />
        </view>
        <view class="btn_box">
          <view class="btn4 btn5" @tap="close()">取消</view>
        </view>
      </view>
    </uni-popup>
    <!-- 起始时间 -->
    <itmister-date-picker
      ref="dateEl1"
      @dateConfirm1="dateConfirm1"
      :hasDay="true"
    ></itmister-date-picker>
    <!-- 结束时间 -->
    <itmister-date-picker
      ref="dateEl2"
      :hasDay="true"
      @dateConfirm1="dateConfirm2"
    ></itmister-date-picker>
  </view>
</template>

<script>
import itmisterDatePicker from "@/components/itmister-date-picker/itmister-date-picker.nvue";
export default {
  components: { itmisterDatePicker },
  data() {
    return {
      type: null,
      chooseType: null,
      type1: "default",
      type2: "default",
      type3: "default",
      weektime: "",
      title1: "下载账单流水",
      subTitle: "导出三个月的账单",
      tagList: [
        {
          type: "success",
          name: "全部",
          isAble: false,
        },
        {
          type: "default",
          name: "红包",
          isAble: false,
        },
        {
          type: "default",
          name: "转账",
          isAble: false,
        },
        {
          type: "default",
          name: "群收款",
          isAble: false,
        },
        {
          type: "default",
          name: "二维码收款",
          isAble: false,
        },
        {
          type: "default",
          name: "商户消费",
          isAble: false,
        },
        {
          type: "default",
          name: "充值提现",
          isAble: false,
        },
        {
          type: "default",
          name: "信用卡还款",
          isAble: false,
        },
        {
          type: "default",
          name: "有退款",
          isAble: false,
        },
      ],
      startDate: "",
      endDate: "",
      typeName: "全部",
    };
  },
  onLoad(options) {
    console.log(this.options, "options");
    // 有ID直接为ID
    if (options.type) {
      this.chooseType = options.type;
    }
    if (this.chooseType == "2") {
      this.title1 = "申请交易明细证明";
      this.subTitle = "一年";
    }
  },
  methods: {
    change(e) {
      console.log(e, "eww");
    },
    // 打开选择器
    selectDate1() {
      this.$refs.dateEl1.show();
    },
    selectDate2() {
      this.$refs.dateEl2.show();
    },
    // 选中日期
    dateConfirm1(date) {
      console.log(date, "123");
      this.startDate = date;
    },
    // 选中日期
    dateConfirm2(date) {
      console.log(date, "123");
      this.endDate = date;
    },
    close() {
      this.$refs.popup1.close();
    },
    setTagListType(name, type) {
      this.typeName = name;
      let types = ["default", "success"];
      this.tagList.forEach((element) => {
        if (element.name == name) {
          let index = types.indexOf(element.type);
          types.indexOf(element.type);
          types.splice(index, 1);
          element.type = types.join(",");
        } else {
          element.type = "default";
        }
      });
      this.close();
    },
    // 获取近一周的时间
    getWeek() {
      // 获取当前日期
      var currentDate = new Date();

      // 获取一周前的日期
      var oneWeekAgo = new Date(
        currentDate.getTime() - 7 * 24 * 60 * 60 * 1000
      );

      // 格式化日期为 "YYYY/MM/DD"
      function formatDate(date) {
        var year = date.getFullYear();
        var month = ("0" + (date.getMonth() + 1)).slice(-2);
        var day = ("0" + date.getDate()).slice(-2);
        return year + "/" + month + "/" + day;
      }

      // 输出时间范围
      var startDate = formatDate(oneWeekAgo);
      var endDate = formatDate(currentDate);
      console.log(startDate + " 至 " + endDate);
      return startDate + " 至 " + endDate;
    },
    getMonth() {
      // 获取当前日期
      var currentDate = new Date();

      // 获取一个月前的日期
      var oneMonthAgo = new Date();
      oneMonthAgo.setMonth(currentDate.getMonth() - 1);

      // 格式化日期为 "YYYY/MM/DD"
      function formatDate(date) {
        var year = date.getFullYear();
        var month = ("0" + (date.getMonth() + 1)).slice(-2);
        var day = ("0" + date.getDate()).slice(-2);
        return year + "/" + month + "/" + day;
      }

      // 输出时间范围
      var startDate = formatDate(oneMonthAgo);
      var endDate = formatDate(currentDate);
      return startDate + " 至 " + endDate;
    },
    toEmail(type) {
      uni.navigateTo({
        url: `/pages/down_bill/choose_emil`,
      });
    },
    setType1(type) {
      let types = ["default", "success"];
      let index = types.indexOf(this.type1);
      types.splice(index, 1);
      this.type1 = types.join(",");
      this.type2 = "default";
      this.type3 = "default";
      this.weektime = this.getWeek();
    },
    setType2(type) {
      let types = ["default", "success"];
      let index = types.indexOf(this.type2);
      types.splice(index, 1);
      this.type2 = types.join(",");
      this.type1 = "default";
      this.type3 = "default";
      this.weektime = this.getMonth();
    },
    setType3(type) {
      let types = ["default", "success"];
      let index = types.indexOf(this.type3);
      types.splice(index, 1);
      this.type3 = types.join(",");
      this.type2 = "default";
      this.type1 = "default";
    },
    handleType() {
      this.$refs.popup1.open("bottom");
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .uni-tag-text--success {
  color: #18ac18 !important;
  border: 1px solid #18ac18 !important;
}
.typeName {
  font-size: 16px;
  margin-left: 15px;
  color: #000;
}
.btn_box {
  width: 100%;
  margin: 20px auto;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 40px;
  .btn5 {
    background-color: #fff !important;
    color: #000 !important;
    font-size: 16px;
    height: 50px;
    width: 100%;
  }
  .btn4 {
    font-size: 16px;
    height: 50px;
    width: 100%;
    line-height: 50px;
    text-align: center;
    color: #fff;
    margin: 0 10px;
    font-size: 18px;
    background-color: #05c160;
    border-radius: 5px;
  }
}

.active {
  background-color: #05c160 !important;
  color: #fff !important;
  height: 45px !important;
  line-height: 45px !important;
}
.tag_box1 {
  margin-left: 10px;
  display: grid; /* 使用网格布局 */
  grid-template-columns: repeat(3, 1fr); /* 创建3列，每列宽度平分 */
  grid-gap: 10px; /* 设置网格间距 */
  .tag_item {
    // padding: 9px 25px;
    text-align: center;
    line-height: 45px;
    height: 45px;
    border: none;
    color: #000;
    background-color: #fff;
    font-weight: 400;
  }
}

.popup-content {
  padding: 20px;
  align-items: center;
  justify-content: center;
  text-align: center;
  background-color: #fafafa;
  .title2 {
    font-size: 14px;
    color: #999;
    margin: 20px 0 15px 0;
  }
  .pop_title {
    font-size: 16px;
    font-weight: 500;
    color: #000;
    margin-bottom: 20px;
  }
}
.container {
  font-size: 14px;
  line-height: 24px;
  .title {
    height: 160px;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    color: #000;
    font-weight: 500;
    font-size: 22px;
    width: 100%;
    text-align: center;
    .sub_title {
      font-size: 18px;
      color: #000;
      font-weight: 400;
      margin-top: 15px;
    }
  }
  .choose_box {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 0 auto;
    width: 93%;

    .item1 {
      display: flex;
      justify-content: start;
      align-items: flex-start;
      position: relative;
      padding: 20px 0;
      width: 100%;
      border-bottom: 1px solid #e0e3ea;
      .toright {
        position: absolute;
        top: 21px;
        right: 10px;
      }
      .tag_box {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        margin-left: 10px;
        .tag_item {
          margin: 0 8px;
        }
        .tag_item1 {
          margin: 0 8px;
          font-size: 12px;
          font-weight: 300;
        }
      }
      .right {
        margin-left: 10px;
        display: flex;
        justify-content: flex-start;
        flex-wrap: nowrap;
        .sdate {
          margin-left: 20px;
        }
        .type_title,
        .sdate {
          color: #000;
          font-weight: 400;
          font-size: 18px;
        }
        .desc {
          margin-top: 10px;
          color: #999;
          font-size: 18px;
          text-align: left;
          width: 256px;
        }
      }
    }
    .time {
      font-size: 14px;
      color: #999;
      text-align: left !important;
      margin-top: 20px;
      width: 100%;
      padding-left: 5px;
    }
  }
  .btn3 {
    width: 220px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #fff;
    font-size: 18px;
    background-color: #18ac18;
    border-radius: 5px;
    position: fixed;
    left: 25%;
    bottom: 100px;
  }
}
</style>
